﻿namespace Chain.BLL
{
    using Chain.IDAL;
    using Chain.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class MemCustomField
    {
        private readonly Chain.IDAL.MemCustomField dal = new Chain.IDAL.MemCustomField();

        public int Add(Chain.Model.MemCustomField model)
        {
            if (!this.ExistTheSameColumnName(model.CustomType, model.CustomFieldName) && !this.Exists(model.CustomField))
            {
                return this.dal.Add(model);
            }
            return -1;
        }

        public DataRow[] CustomGetList(string strWhere)
        {
            return this.GetAllList().Tables[0].Select(strWhere);
        }

        public List<Chain.Model.MemCustomField> DataTableToList(DataTable dt)
        {
            List<Chain.Model.MemCustomField> list = new List<Chain.Model.MemCustomField>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    Chain.Model.MemCustomField item = new Chain.Model.MemCustomField();
                    if ((dt.Rows[i]["CustomFieldID"] != null) && (dt.Rows[i]["CustomFieldID"].ToString() != ""))
                    {
                        item.CustomFieldID = int.Parse(dt.Rows[i]["CustomFieldID"].ToString());
                    }
                    if ((dt.Rows[i]["CustomType"] != null) && (dt.Rows[i]["CustomType"].ToString() != ""))
                    {
                        item.CustomType = int.Parse(dt.Rows[i]["CustomType"].ToString());
                    }
                    if ((dt.Rows[i]["CustomField"] != null) && (dt.Rows[i]["CustomField"].ToString() != ""))
                    {
                        item.CustomField = dt.Rows[i]["CustomField"].ToString();
                    }
                    if ((dt.Rows[i]["CustomFieldName"] != null) && (dt.Rows[i]["CustomFieldName"].ToString() != ""))
                    {
                        item.CustomFieldName = dt.Rows[i]["CustomFieldName"].ToString();
                    }
                    if ((dt.Rows[i]["CustomFieldIsNull"] != null) && (dt.Rows[i]["CustomFieldIsNull"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["CustomFieldIsNull"].ToString() == "1") || (dt.Rows[i]["CustomFieldIsNull"].ToString().ToLower() == "true"))
                        {
                            item.CustomFieldIsNull = true;
                        }
                        else
                        {
                            item.CustomFieldIsNull = false;
                        }
                    }
                    if ((dt.Rows[i]["CustomFieldIsShow"] != null) && (dt.Rows[i]["CustomFieldIsShow"].ToString() != ""))
                    {
                        if ((dt.Rows[i]["CustomFieldIsShow"].ToString() == "1") || (dt.Rows[i]["CustomFieldIsShow"].ToString().ToLower() == "true"))
                        {
                            item.CustomFieldIsShow = true;
                        }
                        else
                        {
                            item.CustomFieldIsShow = false;
                        }
                    }
                    if ((dt.Rows[i]["CustomFieldType"] != null) && (dt.Rows[i]["CustomFieldType"].ToString() != ""))
                    {
                        item.CustomFieldType = dt.Rows[i]["CustomFieldType"].ToString();
                    }
                    if ((dt.Rows[i]["CustomFieldInfo"] != null) && (dt.Rows[i]["CustomFieldInfo"].ToString() != ""))
                    {
                        item.CustomFieldInfo = dt.Rows[i]["CustomFieldInfo"].ToString();
                    }
                    if ((dt.Rows[i]["CustomFieldShopID"] != null) && (dt.Rows[i]["CustomFieldShopID"].ToString() != ""))
                    {
                        item.CustomFieldShopID = int.Parse(dt.Rows[i]["CustomFieldShopID"].ToString());
                    }
                    if ((dt.Rows[i]["CustomFieldCreateTime"] != null) && (dt.Rows[i]["CustomFieldCreateTime"].ToString() != ""))
                    {
                        item.CustomFieldCreateTime = DateTime.Parse(dt.Rows[i]["CustomFieldCreateTime"].ToString());
                    }
                    if ((dt.Rows[i]["CustomFieldUserID"] != null) && (dt.Rows[i]["CustomFieldUserID"].ToString() != ""))
                    {
                        item.CustomFieldUserID = int.Parse(dt.Rows[i]["CustomFieldUserID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Delete(int CustomFieldID)
        {
            return this.dal.Delete(CustomFieldID);
        }

        public bool DeleteList(string CustomFieldIDlist)
        {
            return this.dal.DeleteList(CustomFieldIDlist);
        }

        public bool Exists(string CustomField)
        {
            return this.dal.Exists(CustomField);
        }

        public bool Exists(int CustomFieldID, string CustomFieldName)
        {
            return this.dal.Exists(CustomFieldID, CustomFieldName);
        }

        public bool ExistTheSameColumnName(int CustomType, string CustomFieldName)
        {
            string str = "";
            if (CustomType == 1)
            {
                str = "会员卡号,姓名,性别,身份证号码,手机号码,固定电话,生日,积分,余额,历史消费金额,电子邮箱,地址,会员等级ID,开卡店铺ID,办卡日期,备注,卡面号码";
            }
            else
            {
                str = "商品编码,商品名称,商品简码,商品分类ID,计量单位,参考进价,零售单价,商品积分,商品类型,最低折扣,提成类型,提成金额(比例),商品简介";
            }
            return str.Contains(CustomFieldName);
        }

        public DataSet GetAllCustom()
        {
            return this.dal.GetAllCustom();
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public Chain.Model.MemCustomField GetModel(int CustomFieldID)
        {
            return this.dal.GetModel(CustomFieldID);
        }

        public List<Chain.Model.MemCustomField> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public int Update(Chain.Model.MemCustomField model)
        {
            if (this.Exists(model.CustomFieldID, model.CustomFieldName))
            {
                return -1;
            }
            return this.dal.Update(model);
        }

        public int UpdateCustomFieldShow(string CustomFieldID, bool IsShow)
        {
            return this.dal.UpdateCustomFieldShow(CustomFieldID, IsShow);
        }
    }
}

